<template>
	<view class="yb-checkbox-group yb-flex"
	:id="customIdSync"
	:class="customClassSync"
	:style="rootStyle">
		<slot></slot>
		<view class="mask yb-flex" v-if="disabled" @touchmove.stop.prevent></view>
	</view>
</template>

<script>
	import ComponentMixin from '../../js_sdk/componentMixin.js'
	import Util from '../../js_sdk/util.js'
	export default {
		name: 'YbCheckboxGroup',
		mixins: [ComponentMixin],
		props: {
			value: {
				type: [Array,String,Number,Boolean],
				default () {
					return new Array
				}
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			rootStyle () {
				return this.filterStyle(
					{
						opacity: this.disabled ? 0.5 : 1
					}
				) + this.customStyleSync
			}
		},
		data () {
			return {
				valueSync: []
			}
		},
		mounted() {
			this.valueSync = this.value || []
			this.deepChildValue(this.valueSync)
		},
		methods: {
			onChange (focus, value) {
				let index = this.valueSync.indexOf(value)
				if ( focus ) {
					index > -1 ? this.valueSync[index] = value : this.valueSync.push(value)
				} else {
					index > -1 ? this.valueSync.splice(index, 1) : null
				}
				this.deepChildValue(this.valueSync)
				this.$emit('change', this.valueSync)
				this.$emit('input', this.valueSync)
			},
			deepChildValue (values) {
				let children = Util.getChildrens('YbCheckbox', this)
				if ( Util.typeof(children) == 'Array' ) {
					children.forEach(child => {
						if ( child.$options.name == 'YbCheckbox' ) {
							child.setValue(values)
						}
					})
				}
			}
		},
		watch: {
			value (newVal) {
				this.valueSync = newVal || []
				this.deepChildValue(this.valueSync)
			}
		}
	}
</script>

<style scoped>
	.yb-checkbox-group {
		position: relative;
	}
	.yb-checkbox-group .mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}
</style>